import sys

from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QLabel, QPushButton
from PyQt5.QtGui import QIcon


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.password = ""
        self.init_UI()

    def init_UI(self):
        self.resize(500, 300)
        self.setWindowTitle("抽象按钮的学习1（QAbstractButton)")
        label1 = QLabel("密码输入区", self)
        label1.move(20, 40)
        label2 = QLabel("正确密码：芝麻开门", self)
        label2.move(20, 160)

        label3 = QLabel("你输入的密码：", self)
        label3.move(20, 200)
        self.label4 = QLabel("        ", self)
        self.label4.move(100, 200)

        bt1 = QPushButton("芝", self)
        bt1.move(100, 40)
        bt1.resize(40, 40)

        bt2 = QPushButton("麻", self)
        bt2.move(160, 40)
        bt2.resize(40, 40)
        bt3 = QPushButton("开", self)
        bt3.move(100, 100)
        bt3.resize(40, 40)
        bt4 = QPushButton("门", self)
        bt4.move(160, 100)
        bt4.resize(40, 40)

        bt1.setCheckable(True)
        bt2.setCheckable(True)
        bt3.setCheckable(True)
        bt4.setCheckable(True)

        # bt1.setAutoExclusive(True)
        # bt2.setAutoExclusive(True)
        # bt3.setAutoExclusive(True)
        # bt4.setAutoExclusive(True)

        bt1.clicked.connect(self.setPassword)
        bt2.clicked.connect(self.setPassword)
        bt3.clicked.connect(self.setPassword)
        bt4.clicked.connect(self.setPassword)

        self.show()

    def setPassword(self):
        word = self.sender().text()
        bool_ = self.sender().isCheckable()
        print(bool_)
        if len(self.password) < 4:
            self.password += word
        else:
            self.password = ''
            self.password += word

        self.label4.setText(self.password)
        if len(self.password) and self.password == "芝麻开门":
            QMessageBox.information(self, "提示", "恭喜，密码正确，可以进入！")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
